/*
 * Copyright 2018-present KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jxb.network;


import com.lzy.okgo.model.HttpHeaders;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Create by KunMinX at 19/10/29
 */
public class BaseDataRepository  {
    protected HttpHeaders httpHeaders = new HttpHeaders();
    protected BaseDataRepository() {

    }
    protected void setHeader(String key, String value) {
        httpHeaders.put(key, value);
    }

    public  <B extends BaseResponseBean> void post(String url, HashMap<String,String> params, BaseJsonCallback<B> callback) {
        BaseHttpManager.getInstance().requestPost(url, httpHeaders, params, callback);
    }
    public  <B extends BaseResponseBean> void post(String url, BaseRequestBean requestBean, BaseJsonCallback<B> callback) {
        post(url, requestBean, true, callback);
    }

    public  <B extends BaseResponseBean> void post(String url, HashMap<String, String> map, String key,
                                                                   List<String> filePaths, BaseJsonCallback<B> callback) {
        List<File> files = new ArrayList<>();
        for (String filePath : filePaths) {
            files.add(new File(filePath));
        }
        BaseHttpManager.getInstance().requestPost(url, httpHeaders, null, map, key, files, callback);

    }

    public <B extends BaseResponseBean> void post(String url, BaseRequestBean requestBean, boolean isMediaTypeJson,
                                                                  BaseJsonCallback<B> callback) {
        if (isMediaTypeJson) {
            if (requestBean != null) {
                post(url, requestBean.toJson(), callback);
            }
        } else {
            BaseHttpManager.getInstance().requestPost(url, httpHeaders, requestBean.toMap(), callback);
        }
    }

    public <B extends BaseResponseBean> void post(String url, String key, List<File> files, BaseJsonCallback<B> callback) {
        BaseHttpManager.getInstance().requestPost(url, httpHeaders, null, key, files, callback);
    }

    public <B extends BaseResponseBean> void post(String url, String json, BaseJsonCallback<B> callback) {
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, json);
        BaseHttpManager.getInstance().requestPost(url, httpHeaders, body, callback);
    }

    public <B extends BaseResponseBean> void get(String url, BaseRequestBean requestBean,
                                                                 BaseJsonCallback<B> callback) {
        HashMap<String, String> params = new HashMap<>();
        if (requestBean != null) {
            params = requestBean.toMap();
        }
        get(url,params,callback);
    }

    public <B extends BaseResponseBean> void get(String url, HashMap<String,String> params, BaseJsonCallback<B> callback) {
        BaseHttpManager.getInstance().requestGet(url, httpHeaders, params, callback);
    }
}
